package com.prj.ufdm.cloud.ms.core.bpo._impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.prj.ufdm.cloud.ms.core.bpo.SysAreaBPO;
import com.prj.ufdm.cloud.ms.core.dao.SysAreaDao;
import com.prj.ufdm.cloud.ms.core.model.SysArea;
import com.prj.ufdm.cloud.ms.core.model.SysAreaVO;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhengzixiang
 * @since 2019-09-27
 */
@Service(value="sysAreaBPO")
public class SysAreaBPOImpl extends BaseBPOImpl<SysAreaDao, SysArea>  implements SysAreaBPO {

    @Autowired
    private  SysAreaDao sysAreaDao;

	@Override
	public List<SysArea> getSysAreas(String parentId) throws Exception {
		if(null==parentId) {
			parentId="0";
		}
		
		return getSysAreas(parentId, 0);
	}
	
		//递归取出当前机构下，所有子机构
		private List<SysArea> getSysAreas(String parentId, int num) throws Exception {
			List<SysArea> SysAreas=new ArrayList<SysArea>();
			if(num>10000) {
				return SysAreas;
			}
			SysAreas = sysAreaDao.getSysAreas(parentId);
			if(!CollectionUtils.isEmpty(SysAreas)) {
				for (int i=0;i<SysAreas.size();i++) {
					SysAreas.get(i).setChildren(getSysAreas(SysAreas.get(i).getId(), ++num));
				}
			}
			return SysAreas;		
		}

		@Override
		public List<SysAreaVO> getSysAreaList(String parentId) throws Exception {
			if(null==parentId) {
				parentId="0";
			}
			
			return getSysArea(parentId, 0);
		}
		//递归取出当前机构下，所有子机构
		private List<SysAreaVO> getSysArea(String parentId, int num) throws Exception {
					List<SysAreaVO> SysAreas=new ArrayList<SysAreaVO>();
					if(num>10000) {
						return SysAreas;
					}
					SysAreas = sysAreaDao.getSysArea(parentId);
					if(!CollectionUtils.isEmpty(SysAreas)) {
						for (int i=0;i<SysAreas.size();i++) {
							List<SysAreaVO> children=getSysArea(SysAreas.get(i).getValue(), ++num);
							if(null!=children&&children.size()>0) {
								SysAreas.get(i).setChildren(children);
							}
							
						}
					}
					return SysAreas;		
				}
   
}
